package com.example.hy2110szyx.controller;

import com.example.hy2110szyx.common.DatatablesJSON;
import com.example.hy2110szyx.common.MyJsonResult;
import com.example.hy2110szyx.entity.Distribution;
import com.example.hy2110szyx.entity.FlightInfo;
import com.example.hy2110szyx.entity.UserInfo;
import com.example.hy2110szyx.service.DistributionService;
import com.example.hy2110szyx.vo.PageVo;
import com.github.pagehelper.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping(value = "/distribution", produces = "application/json")
@Slf4j
@CrossOrigin(origins = "*")
public class DistributionController {

    private DistributionService distributionService;

    @Autowired
    public DistributionController(DistributionService distributionService) {
        this.distributionService = distributionService;
    }

    @GetMapping("/query/{fno}")
    @ResponseStatus(HttpStatus.OK)
    public MyJsonResult queryDistri(@PathVariable("fno") String fno) {
        Distribution distribution = distributionService.queryByFno(fno);
        return new MyJsonResult(0, null, null, distribution);
    }

    @GetMapping("/page")
    @ResponseBody
    public DatatablesJSON<Distribution> query(
            int draw,
            @RequestParam("order[0][column]") int orderColumn,
            @RequestParam("order[0][dir]") String orderDir,
            int start, int length, String fno
    ) throws ParseException {
        String[] columns = {null, "dno", "fno", "cno", "seat","price", null};
            Distribution disConn = new Distribution(fno);
            log.debug("order[0][column]=" + orderColumn);
            log.debug("order[0][dir]=" + orderDir);
            log.debug("disConn is {}", disConn);
            PageVo<Distribution> pageVo = new PageVo(start, length, columns[orderColumn], orderDir, disConn);
            Page<Distribution> page = distributionService.queryAllDisForPage(pageVo);
            DatatablesJSON<Distribution> datatablesJSON = new DatatablesJSON<>();
            datatablesJSON.setDraw(draw);
            datatablesJSON.setRecordsFiltered(page.getTotal());
            datatablesJSON.setData(page.getResult());
            return datatablesJSON;

    }


    @PutMapping("/modDistri/{id}")
    @ResponseStatus(HttpStatus.OK)
    public MyJsonResult modOneDistribution(@PathVariable Integer id, @RequestBody Distribution distribution) {
        distribution.setId(id);
        distributionService.updateOneDistribution(distribution);
        return new MyJsonResult(0, null, null, distribution);
    }

//    @PostMapping("/distribution")
//    @ResponseStatus(HttpStatus.CREATED)
//    public MyJsonResult insertOneDistribution(@RequestBody Distribution distribution) {
//        flightService.insertOneDistribution(distribution);
//        return new MyJsonResult(0, null, null, distribution);
//    }

//    @DeleteMapping("/deldistri/{id}")
//    @ResponseStatus(HttpStatus.OK)
//    public MyJsonResult delOneDistribution(@PathVariable Integer id) {
//        flightService.delOneDistribution(id);
//        return new MyJsonResult(0, null, null, null);
//    }
}
